package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.DataTime;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 分页查询
     *
     * @param dataTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageQuery(DataTime dataTime) {
        log.info("获取到的数据为:{}", dataTime);
        Page<Orders> page = new Page<>(dataTime.getPage(), dataTime.getPageSize());
        LambdaUpdateWrapper<Orders> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(dataTime.getNumber() != null, Orders::getNumber, dataTime.getNumber());
        lqw.between(dataTime.getBeginTime() != null, Orders::getOrderTime, dataTime.getBeginTime(), dataTime.getEndTime());
        Page<Orders> ordersPage = ordersService.page(page, lqw);
        return R.success(ordersPage);
    }

    @PutMapping
    public R<String> setStatus(@RequestBody Orders orders) {
        LambdaUpdateWrapper<Orders> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(Orders::getId, orders.getId());
        lqw.set(Orders::getStatus, orders.getStatus());
        ordersService.update(lqw);
        return R.success("修改成功!");
    }
}